package com.controller;

import com.pojo.TbProducts;
import com.service.ProductsService;
import com.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;

@RestController
@RequestMapping("/api/products")
public class ProductsController {
    @Autowired
    JSONResult jsonResult;
    @Autowired
    ProductsService productsService;

    @GetMapping("/findAllProductByCode")
    public JSONResult findAllProductByCode(@RequestParam("page") int page,@RequestParam("limit") int limit,@RequestParam(value = "code",defaultValue = "") String code){
        return productsService.findAllProductByCode(page,limit,code);
    }
    @GetMapping("/findAllProduct")
    public JSONResult findAllProduct(@RequestParam("page") int page,@RequestParam("limit") int limit){
        return productsService.findAllProduct(page,limit);
    }
    @GetMapping("/findAllProductByTime")
    public JSONResult findAllProductByTime(@RequestParam("page") int page,@RequestParam("limit")int limit,@RequestParam("starttime")String starttime,@RequestParam("endtime")String endtime) throws ParseException {
        return productsService.findAllProductByTime(page,limit,starttime,endtime);
    }

    @PostMapping("/addProduct")
    public JSONResult addProduct(@RequestBody TbProducts products){
        return productsService.addProduct(products);
    }

    @GetMapping("/deleteProduct")
    public JSONResult deleteProduct(@RequestParam("pid")int pid){
        return productsService.deleteProduct(pid);
    }

    @PostMapping("/updateProduct")
    public JSONResult updateProduct(@RequestBody TbProducts products){
        return productsService.updateProduct(products);
    }

    @GetMapping("/findProductById")
    public JSONResult findProductById(@RequestParam("pid")int pid){
        return productsService.findProductById(pid);
    }

    @GetMapping ("/findProductByAttention")
    public JSONResult findProductByAttention(@RequestParam("page")int page,@RequestParam("limit")int limit,@RequestParam(value = "attention",defaultValue = "-1")int attention){
        return productsService.findProductByAttention(page,limit,attention);
    }

    @GetMapping("/updateState")
    public JSONResult updateState(@RequestParam("pid")int pid,@RequestParam("state")int state){
        return productsService.updateState(pid,state);
    }
    @PostMapping("/upload")
    public JSONResult upload(MultipartFile file) {
        JSONResult upload = productsService.upload(file);
        return upload;
    }
    @GetMapping("/updateAttention")
    public JSONResult updateAttention(@RequestParam("pid")int pid,@RequestParam("attention")int attention){
        return productsService.updateAttention(pid,attention);
    }
}
